<?php
/**
 * V4
 *
 */
class data_rss_article extends data_article {
	public function __construct() {
		parent::__construct();
	}
	/**
	 * rss中一篇文章的详情
	 *
	 * @param int $id			// rss article表的主键id
	 * @return array
	 */
	public function one ($id) {
		$id		= intval($id);
		
		if ($id) {
			$execute = $this->db->execute('
				select
					rss_article.id				AS id,
					0							AS article_id,
					rss_article.id				AS content_id,
					0							AS user_id,
					rss_article.title			AS title,
					rss_article.dt				AS dt,
					article_url.id				AS url_id,
					article_url.url				AS url
				from
					rss_article, article_url
				where
					rss_article.id		= "'.$id.'"			and
					article_url.id		= rss_article.url_id
			');
			return $execute[0];
		} else return false;
	}
	public function ls ($rss_id, $limit=10) {
		$rss_id = intval($rss_id);
		$limit	= intval($limit);
		$limit	= ($limit) ? $limit : 1;
		
		if ($rss_id) {
			$execute = $this->db->execute('
				select
					rss_article.*,
					article_url.url
				from
					rss_article, article_url
				where
					rss_article.rss_id	= "'.$rss_id.'"		and
					article_url.id		= url_id
				order by
					rss_article.id DESC
				limit
					'.$limit.'
			');
			return $execute;
		} else return false;
	}
	//	#################### Auto Update Rss #############################
	/**
	 * @ auto_update_rss_article_from_remote.class -> __construct()
	 * 
	 * 更新rss文章时，检查并更新从rss获得的文章
	 * 如果文章url和rss_id存在，检查内容有无变化，如有变化，更新
	 * 如果文章url不存在，添加该rss文章
	 *
	 * @param Int $rss_id
	 * @param String $url
	 * @param String $title
	 * @param String $description
	 * @param String $content
	 * @param Date $pubdate
	 * @return bool
	 */
	public function update_or_add ($rss_id, $url, $title, $description, $content, $pubdate) {
		$rss_id 		= intval($rss_id);
		$url			= url_cut_tail($url);
		$title			= strip_tags(trim($title));
		$description	= trim($description);
		$content		= trim($content);
		
		// content和description哪个长，用哪个
		$content = (strlen($content) > strlen($description)) ? $content : $description;
		
		if ($rss_id and $url and $title and $content) {
			// 检查在article表中是否存在该url -----------------------------------------------------------
			$exist = $this->is_exist($rss_id, $url);		// 查询文章详情，检查是否有修改
			if ($exist) {																				// 文章已经存在，检查内容是否一致，如果不一致，更新
				if ($exist['title'] != $title or $exist['content'] != $content) {
					if ($exist['title'] != $title) {
						$update_title = $this->update($exist['id'], $title);							// 内容不一致，更新article内容
					}
					if ($exist['content'] != $content) {
						$update_content = $this->edit_rss_article_content($exist['id'], $content);
					}
					return ($update_title or $update_content);
				} else {
					return true;																				// 内容一致，不用更新, nothing ...
				}
			} else {
				return $this->add($rss_id, $url, $title, $content, $pubdate);		// 不存在，写入，并返回insert id作为article id
			}
		} else return false;
	}
	/**
	 * 写入rss文章
	 *
	 * @param int $rss_id
	 * @param string $url
	 * @param string $title
	 * @param string $description
	 * @param string $content
	 * @param DateTime $pubdate
	 * @return int
	 */
	private function add ($rss_id, $url, $title, $content, $pubdate) {
		$rss_id			= intval($rss_id);
		$url_id 		= $this->url_to_id_or_insert($url);
		$pubdate		= date('Y-m-d H:i:s', strtotime($pubdate));
		$title			= addslashes($title);
		
		if ($rss_id and $url_id and $title and $content) {
			// 写入rss article
			$insert_id = $this->db->execute('
				insert into
					rss_article
				set
					rss_id		= "'.$rss_id.'",
					url_id		= "'.$url_id.'",
					title		= "'.$title.'",
					dt			= "'.$pubdate.'"
			');
			if ($insert_id) {
				// 写入 rss article content
				$this->add_rss_article_content($id, $content);
				// 发布event
				$data_event = new data_event();
				$data_event->add('add_rss_article', $insert_id);
				
				return $insert_id;
			} else return false;
		} else return false;
	}
	/**
	 * 更新rss来源的文章，同时更新rss_article_content和rss_article两个表
	 *
	 * @param int $id
	 * @param string $title
	 * @param string $description
	 * @param string $content
	 * @return bool
	 */
	private function update ($id, $title) {
		$title = addslashes($title);
		return $this->db->execute('
			update
					rss_article
				set
					title	= "'.$title.'"
				where
					id	= "'.$id.'"
		');
	}
	/**
	 * 检查某一个rss中的文章url是否已经存在，存在时返回rss_article_content表中的详情
	 *
	 * @param Int $rss_id
	 * @param String $url
	 * @return Array / false
	 */
	private function is_exist ($rss_id, $url) {
		$rss_id 	= intval($rss_id);
		$url_id		= $this->is_url_exist($url);		// 检查url是否存在，如果url都不存在的话，那么文章也肯定不存在
		
		if ($rss_id and $url_id) {						// url存在的话，那么检查文章
			$execute = $this->db->execute('
				select 
						*
					from
						rss_article
					where
						url_id		= "'.$url_id.'"		and
						rss_id		= "'.$rss_id.'"
					limit
						1
			');
			if ($execute[0]) {
				$execute[0]['content'] = $this->get_rss_article_content($execute[0]['id']);
				return $execute[0];
			} else return false;
		} else return false;
	}
}
?>